﻿/*
   Author: Tomer Paz
   games.com
   2009
*/

/* Resign object */

package monopoly.views {
	import fl.controls.Button;
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.EventDispatcher;
	import flash.text.TextFormat;
	import flash.text.TextFieldAutoSize;
	import monopoly.GameController.Dialogs.Templates.BasicDialog;
	import monopoly.GameController.Dialogs.IDialog;
	import monopoly.Localization.*;
	
	public class ResignView extends Sprite implements IDialog
		{
		public static const RESIGN:uint	= 1;

		protected var _rbutton:Button = new Button();
		protected var _chosen_action:uint = 0;
		protected var _rstage:Stage;
		protected var _myStrings:ILocalLanguage = LocalLanguage.getInstance();
		
		public function ResignView(rootStage:Stage)
			{
			setupButton();
			_rstage = rootStage;
			}
		public function enableResign():void
			{
			_rbutton.enabled = true;
			}
		public function get chosenAction():uint
			{
			return _chosen_action;
			}
			
		private function setupButton():void
			{
			_rbutton.enabled	= false;
			_rbutton.label		= _myStrings.MsgResignLabel;
			_rbutton.textField.autoSize = TextFieldAutoSize.LEFT;
			_rbutton.setSize(25,20);
			//_rbutton.textField.defaultTextFormat = 
			
			_rbutton.addEventListener(MouseEvent.CLICK, onButtonClick);
			addChild(_rbutton);
			}
		private function onButtonClick(e:MouseEvent):void
			{
			var dialog:BasicDialog = new BasicDialog(_myStrings.MsgResignTitle, 
											   _myStrings.MsgResignText,
											   _myStrings.MsgResignYes, _myStrings.MsgResignNo);
			dialog.addEventListener(MouseEvent.CLICK, onResignDecision);
			dialog.x = _rstage.width  * 0.5;
			dialog.y = _rstage.height * 0.5;
			_rstage.addChild(dialog);
			}
		private function onResignDecision(e:MouseEvent):void
			{
			var dialog:BasicDialog = e.currentTarget as BasicDialog;
			
			if ( e.target is Button )
				{
				if ( (e.target as Button).label == _myStrings.MsgResignYes )
					{
					_chosen_action = RESIGN;
					_rbutton.enabled = false;
					_rbutton.removeEventListener(MouseEvent.CLICK, onButtonClick);
					}
				else if ( (e.target as Button).label == _myStrings.MsgResignNo )
					{
					_chosen_action = 0;
					}
			
				_rstage.removeChild(dialog);
				dialog.removeEventListener(MouseEvent.CLICK, onResignDecision);

				dispatchEvent(new Event(Event.COMPLETE));
				}
			}
		}
	
}
